﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Audit_Configuration_Oracle.Controllers;

namespace Audit_Configuration_Oracle.Views
{
    public partial class ACO_Audit_History : Form
    {
        public ACO_Audit_History()
        {
            InitializeComponent();
        }

        private void ACO_Audit_History_Load(object sender, EventArgs e)
        {
            try
            {
                List<string> userList = NewRuleController.GetUserList();
                userList.Insert(0, "");
                cboSchema.Items.AddRange(userList.ToArray());

                List<string> tableList = NewRuleController.GetTableList();
                tableList.Insert(0, "");
                cboObject.Items.AddRange(tableList.ToArray());
                LoadChckListColumns();
                cboSchema.SelectedIndex = 0;
                cboObject.SelectedIndex = 0;
                chkDefault.Checked = true;
                cbAnyDate.Checked = true;
                dtpFechaInicio.Enabled = false;
                dtpFechaFinal.Enabled = false;
            }
            catch (Exception error)
            {

            }
        }

        private void btnConsulta_Click(object sender, EventArgs e)
        {
            string dateStart = string.Empty;
            string dateEnd = string.Empty;
            string user = string.Empty;
            string obj = string.Empty;
            DataSet ds = null;
            List<string> selectedColumns = new List<string>();
            try
            {
                for (int x = 0; x <= chkListColumns.CheckedItems.Count - 1; x++)
                {
                    selectedColumns.Add(chkListColumns.CheckedItems[x].ToString());
                }

                //date = default(DateTime);
                if (!cbAnyDate.Checked)
                {
                    dateStart = dtpFechaInicio.Value.ToString();
                    dateEnd = dtpFechaFinal.Value.ToString();
                }
                else
                {
                    dateStart = "";
                    dateEnd = "";
                }

                if (cboSchema.SelectedIndex != 0)
                {
                    user = cboSchema.SelectedItem.ToString();
                }
                if (cboObject.SelectedIndex != 0)
                {
                    obj = cboObject.SelectedItem.ToString();
                }
                ds = HistoryController.GetResutls(dateStart, dateEnd, user, obj, selectedColumns);
                this.grdResultados.DataSource = ds.Tables[0];
            }
            catch (Exception error)
            {
                MessageBox.Show("Ocurrio un error al realizar la consulta. Intente de nuevo",
                    "Consulta Incompleta", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            try
            {
                cboSchema.SelectedIndex = 0;
                cboObject.SelectedIndex = 0;

                if (!cbAnyDate.Checked)
                {
                    cbAnyDate.Checked = true;
                    dtpFechaInicio.Value = DateTime.Now.AddDays(-1);
                    dtpFechaFinal.Value = DateTime.Now;
                }
            }
            catch { }
        }

        private void LoadChckListColumns()
        {
            List<string> columns = null;
            try
            {
                columns = HistoryController.GetAuditColumns();
                this.chkListColumns.Items.AddRange(columns.ToArray());
                this.chkListColumns.MultiColumn = true;
            }
            catch (Exception e)
            {
                MessageBox.Show("No se pudieron recuperar las columnas de auditoría, se utilizárá la configuración por defecto",
                   "Consulta Incompleta", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }


        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (cbAnyDate.Checked)
            {
                dtpFechaInicio.Enabled = false;
                dtpFechaFinal.Enabled = false;
            }
            else
            {
                dtpFechaInicio.Enabled = true;
                dtpFechaFinal.Enabled = true;
            }
        }

        private void chkDefault_CheckedChanged(object sender, EventArgs e)
        {           
            /*string[] defaultData = HistoryController.DEFAULT_DATA;
            string item = string.Empty;
            for (int i = 0; i < chkListColumns.Items.Count; i++)
            {
                item = chkListColumns.Items[i].ToString().ToUpper();
                if (defaultData.Contains<string>(item))
                {
                    chkListColumns.SetItemChecked(i, chkDefault.Checked);
                }
            }*/
           SetDefault(chkDefault.Checked);  
        }

        private void resetAllControls()
        {
            btnClean_Click(this, null);
            resetChecks();
            grdResultados.DataSource = null;
        }

        private void resetChecks()
        {
            CleanAllColumns();
            if (chkDefault.Checked)
            {
                SetDefault(true);
            }
            chkDefault.Checked = true;
            //SetDefault(true);
        }

        private void CleanAllColumns()
        { 
            for (int i = 0; i < chkListColumns.Items.Count; i++)
            {            
                chkListColumns.SetItemChecked(i, false);                
            }
        }

        private void SetDefault(bool status)
        {         
            string[] defaultData = HistoryController.DEFAULT_DATA;
            string item = string.Empty;
            for (int i = 0; i < chkListColumns.Items.Count; i++)
            {
                item = chkListColumns.Items[i].ToString().ToUpper();
                if (defaultData.Contains<string>(item))
                {
                    chkListColumns.SetItemChecked(i, status);
                }
            }
        }

        private void ACO_Audit_History_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
            resetAllControls();
        }
    }
}
